package com.pg.ffmpeg;

import android.util.Log;

import com.google.gson.Gson;
import com.pg.ffmpeg.bean.VideoInfo;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class RecodeUtil {

    static {
        System.loadLibrary("native-lib");
    }

    private static ExecutorService mSingleThreadPool = Executors.newSingleThreadExecutor();
    private RecodeCallback mCallback;
    private static final RecodeUtil ourInstance = new RecodeUtil();

    public static RecodeUtil getInstance() {
        return ourInstance;
    }

    private RecodeUtil() {
    }

    public void runCommand(String cmd,RecodeCallback callback){
        if(cmd == null || cmd.length() == 0) {
            if (callback != null) {
                callback.onFail("cmd is null");
            }
            return ;
        }

        String[] args = cmd.split(" ");
        for(int i = 0; i < args.length; i++) {
            Log.d("ffmpeg-jni", args[i]);
        }
        try {
            mSingleThreadPool.execute(new TranscodeRunnable(callback,args));
        } catch (Exception e) {
            if (callback != null) {
                callback.onFail(e.getMessage());
            }
        }
    }

    public VideoInfo getVideoInfo(String path){
        String s = null;
        try {
            s = retrieveInfo(path);
        } catch (Exception e) {
            e.printStackTrace();
        }
        VideoInfo info = new VideoInfo();
        if (s!=null){
            info = new Gson().fromJson(s, VideoInfo.class);
        }
        return info;
    }

    /**
     * @return 1初始化中 2转码中 3完成 4失败
     */
    public native int getState();

    private native int runCommand(int argc, String[] args);

    private native double getProgress();

    private native String getErrorInfo();

    private native String retrieveInfo(String path);

    private static class TranscodeRunnable implements Runnable{
        private RecodeCallback callback;
        private String[] args;
        TranscodeRunnable(RecodeCallback callback, String[] args) {
            this.callback = callback;
            this.args = args;
        }

        @Override
        public void run() {
            new Thread(new Runnable(){

                @Override
                public void run() {
                    if (callback != null) {
                        int state = getInstance().getState();
                        while (state != 3 && state != 4) {
                            double progress = getInstance().getProgress();
                            callback.onProgress((int) Math.round(progress));
                            try {
                                Thread.sleep(500);
                            } catch (InterruptedException e) {
//                                e.printStackTrace();
                            }
                            state = getInstance().getState();
                        }
                        callback.onComplete();
                    }
                }
            }).start();
            int result = getInstance().runCommand(args.length,args);
            if (result != 0 && result != 255){
                if (callback != null){
                    callback.onFail(getInstance().getErrorInfo());
                }
            }
        }
    }

}
